<?php
/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
*/

include (__DIR__.'/../index.php');

class FormatTest extends \libs\PHPCan\MysqlActions
{
	var $query = array();
	var $execute = array('gmaps');
	var $disabled = array();

	private function setQuery () {
		$this->query = array(
			'table' => 'formats',
			'data' => array()
		);
	}

	public function testInsertFormatBoolean () {
		$format = 'boolean';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// CORRECT
		$values = array(0, 1, true, false, 'true', 'false', null);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$values = array(-1, 'a', '');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatCodefile () {
		$format = 'codefile';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		$uploads = fileWeb('uploads').'formats/'.$format;

		if (is_dir($uploads) && !is_writable($uploads) && !chmod($uploads, 0777)) {
			die('Can not set 777 permissions to '.$uploads.' folder');
		}

		// CORRECT
		$values = array('', true, false, null, 1, 1000, '1000', $this->Lipsum->getContent(1000, 'plain'), $this->Lipsum->getContent(1000, 'html'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		if (!chmod($uploads, 0555)) {
			die('Can not set 555 permissions to '.$uploads.' folder');
		}

		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(false, null, '', 'a');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatDate () {
		$format = 'date';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// CORRECT
		// http://www.php.net/manual/en/datetime.formats.date.php
		// Not all valid dates formats are valid, only well formed like 2011/-.02/-.06
		$values = array('', false, null, '2011-11-22', '2011/11/22', '22-11-2011', '22.11.11', '22.11.2011');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(
			false, null, '', 'a', 2011, '11-22', '11/22', '2011-11', '2011-11-22 23:59:59',
			'2011/11', '2011:11:22', '22/11/2011', '11.22.2011', '2011-11-22 25:59:59'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatDateTime () {
		$format = 'datetime';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// CORRECT
		// http://www.php.net/manual/en/datetime.formats.date.php
		// http://www.php.net/manual/en/datetime.formats.time.php
		// Not all valid dates formats are valid, only well formed like 2011/-.02/-.06 00:00:00
		$values = array(
			'', false, null, '2011-11-22 23:59:59', '2011/11/22 23:59:59', '22-11-2011 23:59:59',
			'22.11.2011 23:59:59', '2011-11-22 23:59:59', '2011-11-22 24:59:59'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(
			false, null, '', '11/22', '2011-11', '2011-11-22', '2011/11/22', '22-11-2011', '22.11.11',
			'22.11.2011', '23:59:59', '11/22 23:59:59', '2011-11 23:59:59', 'a', '11-22', '2011/11',
			'2011:11:22', '22/11/2011', '11.22.2011', '2011-11-22 25:59:59', '11-22 23:59:59',
			'2011/11 23:59:59', '22/11/2011 23:59:59', '22.11.11 23:59:59', '11.22.2011 23:59:59',
			'2011-11-22 25:59:59', '2011:11:22 23:59:59'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatEmail () {
		$format = 'email';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		// http://en.wikipedia.org/wiki/Email_address
		// http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
		$values = array(
			'', false, null, 'a@domain.com', '12@domain-12.com', 'a12a@12-domain.com', 'a-12@12.com', 'a.12@domain.12.com', 'a_12@12-domain.com',
			'a@12-a.com', 'name/a@a.co.uk', 'name_a-a.12@domain-a.com', 'a-12@a-12.com', 'a.12@a.12.com', 'a_12@a-12.com',
			'"Abc\@def"@example.com', '"Fred\ Bloggs"@example.com', '"Joe\\Blow"@example.com', '"Abc@def"@example.com', '""@example.com',
			'customer/department=shipping@example.com', '$A12345@example.com', '!def!xyz%abc@example.com', '_somename@example.com'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(
			false, null, '', true, 'a_12@12_domain.com', 'NotAnEmail', '@NotAnEmail', '""test\blah""@example.com',
			'"test\rblah\"@example.com', '""test""blah""@example.com', '.wooly@example.com', 'wo..oly@example.com',
			'pootietang.@example.com', 'example.com', '@example.com', '.@example.com', 'Ima Fool@example.com'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatEnum () {
		$format = 'enum';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		// predefined valid values: option1, option2
		$values = array(false, null, '', 'option1', 'option2');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(1, '0', '1', 'option3');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatFile () {
		$format = 'file';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		$uploads = fileWeb('uploads').'formats/'.$format;

		if (is_dir($uploads) && !is_writable($uploads) && !chmod($uploads, 0777)) {
			die('Can not set 777 permissions to '.$uploads.' folder');
		}

		$tmpfile1 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile1, $this->Lipsum->getContent(10000));

		$tmpfile2 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile2, $this->Lipsum->getContent(10000));

		$_file = array(
			'error' => 0,
			'tmp_name' => $tmpfile2,
			'name' => (trim(str_replace('.', '', $this->Lipsum->getContent(2, 'plain'))).'.txt'),
			'size' => filesize($tmpfile2)
		);

		// VALID
		$values = array(false, null, '', '0', '1', $tmpfile1, $_file, 'http://example.com/', 'http://example.com');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		if (is_dir($uploads) && !chmod($uploads, 0555)) {
			die('Can not set 555 permissions to '.$uploads.' folder');
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(true, false, null, '', 1, $_file);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}

		if (is_file($tmpfile1)) {
			unlink($tmpfile1);
		} if (is_file($tmpfile2)) {
			unlink($tmpfile2);
		}
	}

	public function testInsertFormatFloat () {
		$format = 'float';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(true, false, null, '', 1, '1', '1,6', 1.6, '1.6', '0.6', -10.60, '-10,60', '+10,60');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(false, null, '', '0.00.0', '0,00.0', '1,2.2', '1.2,2', '-10..60', '+-10', '-20,.60', '-20,,60');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatGmaps () {
		$format = 'gmaps';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(
			false, null, '',
			array('x' => 0, 'y' => 0, 'z' => 1),
			array('x' => 1, 'y' => 1, 'z' => 1),
			array('x' => 1, 'y' => -1, 'z' => 1),
			array('x' => -1, 'y' => 1, 'z' => 1),
			array('x' => -1, 'y' => -1, 'z' => 1),
			array('x' => 90, 'y' => -90, 'z' => 15),
			array('x' => -90, 'y' => 90, 'z' => 15),
			array('x' => -90, 'y' => -90, 'z' => 15)
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true), 'z');

		$values = array(array('x' => 0, 'y' => 0, 'z' => 1));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$values = array(
			 false, null, '',
			array('x' => 1, 'y' => 1, 'z' => 0),
			array('x' => 1, 'y' => 1, 'z' => -1),
			array('x' => 1, 'y' => -1, 'z' => 22),
			array('x' => 181, 'y' => 1, 'z' => 1),
			array('x' => -181, 'y' => 1, 'z' => 1),
			array('x' => 1, 'y' => 91, 'z' => 1),
			array('x' => 1, 'y' => -91, 'z' => 1),
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatHtml () {
		$format = 'html';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(
			'', true, false, null, 1, 1000, '1000',
			$this->Lipsum->getContent(1000, 'plain'), $this->Lipsum->getContent(1000, 'html'), 
			('<script> document.write("'.$this->Lipsum->getContent(1000, 'html').'"); </script>')
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		$result = $this->Db->select(array(
			'table' => 'formats',
			'fields' => $format,
			'sort' => 'id DESC',
			'limit' => 1
		));

		$this->assertFalse(strstr($result[$format], '<script>') ? true : false, $this->message($result[$format]));

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array('', false, null);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatId_text () {
		$format = 'id_text';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array('', true, false, null, 1, 1000, '1000', $this->Lipsum->getContent(5, 'plain'), $this->Lipsum->getContent(5, 'html'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array('', false, null, $this->Lipsum->getContent(50, 'plain'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatImage () {
		$format = 'image';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		$uploads = fileWeb('uploads').'formats/'.$format;

		if (is_dir($uploads) && !is_writable($uploads) && !chmod($uploads, 0777)) {
			die('Can not set 777 permissions to '.$uploads.' folder');
		}

		$tmpfile1 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile1, file_get_contents('http://lorempixum.com/100/100/'));

		$tmpfile2 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile2, file_get_contents('http://lorempixum.com/100/100/'));

		$tmpfile3 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile3, 'http://lorempixum.com/100/100/');

		$_file1 = array(
			'error' => 0,
			'tmp_name' => $tmpfile2,
			'name' => (trim(str_replace('.', '', $this->Lipsum->getContent(2, 'plain'))).'.txt'),
			'size' => filesize($tmpfile2)
		);

		$_file2 = array(
			'error' => 0,
			'tmp_name' => $tmpfile3,
			'name' => (trim(str_replace('.', '', $this->Lipsum->getContent(2, 'plain'))).'.txt'),
			'size' => filesize($tmpfile3)
		);

		// VALID
		$values = array(false, null, '', '0', '1', $tmpfile1, $_file1, 'http://lorempixum.com/100/100/');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		if (is_dir($uploads) && !chmod($uploads, 0555)) {
			die('Can not set 555 permissions to '.$uploads.' folder');
		}

		// FAIL
		$tmpfile2 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile2, 'http://lorempixum.com/100/100/');

		$tmpfile3 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile3, 'http://lorempixum.com/100/100/');

		$_file2 = array(
			'error' => 0,
			'tmp_name' => $tmpfile3,
			'name' => (trim(str_replace('.', '', $this->Lipsum->getContent(2, 'plain'))).'.jpg'),
			'size' => filesize($tmpfile3)
		);

		$values = array($tmpfile2, $_file2, 'http://example.com/');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}

		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(true, false, null, '', 1, $tmpfile1, $_file1, 'http://lorempixum.com/100/100/');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}

		if (is_file($tmpfile1)) {
			unlink($tmpfile1);
		} if (is_file($tmpfile2)) {
			unlink($tmpfile2);
		} if (is_file($tmpfile3)) {
			unlink($tmpfile3);
		}
	}

	public function testInsertFormatInteger () {
		$format = 'integer';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(true, false, null, '', 1, '1', '1', 99999999, '999999999');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(false, null, '', '1,1', 1.1, 0.1, '-1', -1, 'a');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatIp () {
		$format = 'ip';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(
			false, null, '', '192.168.0.1', '127.0.0.1', '255.255.255.255',
			'2001:0db8:85a3:08d3:1319:8a2e:0370:7334', '2001::1', '2a01:198:433::48',
			'805B:2D9D:DC28:0000:0000:FC57:D4C8:1FFF', '805B:2D9D:DC28::FC57:D4C8:1FFF',
			'805B:2D9D:DC28::1', '3FFE:604:6:7::2'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(
			true, false, null, '', 'string', '192.168.0.256', '192.168.0', '127.0.0.-1', '256.0.0.0',
			'G001:470:XXXX:0:54f9:df39:8aee:6c16/48', '3FFE:604:6:7::2/126', '805B:2D9D:DC28:::/48',
			'G001:470:XXXX:0:54f9:df39:8aee:6c16', '3FFE:604::6:7::2', '805B:2D9D:DC28:::/48'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatPassword () {
		$format = 'password';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(false, null, '', 'aaaaa', '12345', '|@#%&/()=?¿', md5(1));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(true, false, null, '', '1234', 'aaaa', (md5(1).'a'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatRealfile () {
		$format = 'realfile';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		$uploads = fileWeb('uploads').'formats/'.$format;

		if (is_dir($uploads) && !is_writable($uploads) && !chmod($uploads, 0777)) {
			die('Can not set 777 permissions to '.$uploads.' folder');
		}

		$tmpfile1 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile1, $this->Lipsum->getContent(10000));

		$tmpfile2 = tempnam(sys_get_temp_dir(), 'UnitTests');

		file_put_contents($tmpfile2, $this->Lipsum->getContent(10000));

		$_file = array(
			'error' => 0,
			'tmp_name' => $tmpfile2,
			'name' => (trim(str_replace('.', '', $this->Lipsum->getContent(2, 'plain'))).'.txt'),
			'size' => filesize($tmpfile2)
		);

		// VALID
		$values = array(false, null, '', '0', '1', $tmpfile1, $_file, 'http://example.com/', 'http://example.com');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		if (is_dir($uploads) && !chmod($uploads, 0555)) {
			die('Can not set 555 permissions to '.$uploads.' folder');
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(true, false, null, '', 1, $_file);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}

		if (is_file($tmpfile1)) {
			unlink($tmpfile1);
		} if (is_file($tmpfile2)) {
			unlink($tmpfile2);
		}
	}

	public function testInsertFormatSort () {
		$format = 'sort';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		$object = $this->Db->getTable('formats')->getFormat('sort');

		// VALID
		$values = array(true, false, null, '', 1, '1', '1', 99999, '999999');

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));

			$object->afterSave($this->Db, array(
				'id' => $result,
				'old_value' => array('' => ''),
				'new_value' => array('' => $value)
			));
		}

		$result = $this->Db->select(array(
			'table' => 'formats',
			'fields' => $format
		));

		$cnt = 0;

		foreach ($result as $result_value) {
			if ($result_value['sort'] == '1') {
				$cnt++;
			}
		}

		$this->assertEquals(1, $cnt);

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array(false, null, '', '1,1', 1.1, 0.1, '-1', -1, 'a', 9999999999);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatText () {
		$format = 'text';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array('', true, false, null, 1, 1000, '1000', $this->Lipsum->getContent(1000, 'plain'), $this->Lipsum->getContent(1000, 'html'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));

			$result = $this->Db->select(array(
				'table' => 'formats',
				'fields' => $format,
				'conditions' => array(
					'id' => $result
				),
				'limit' => 1
			));

			$this->assertFalse(preg_match('/<[^>]+>$/', $result[$format]) ? true : false, $this->message($result[$format]));
		}

		$this->Db->getTable('formats')->setFormatSettings($format, array('raw' => true));

		$this->query['data'][$format] = ('<script> document.write("'.$this->Lipsum->getContent(1000, 'html').'"); </script>');

		$result = $this->Db->insert($this->query);

		$this->assertInternalType('integer', $result, $this->message($value));

		$result = $this->Db->select(array(
			'table' => 'formats',
			'fields' => $format,
			'conditions' => array(
				'id' => $result
			),
			'limit' => 1
		));

		$this->assertTrue(strstr($result[$format], '<script>') ? true : false, $this->message($result[$format]));
		$this->assertTrue(preg_match('/<[^>]+>$/', $result[$format]) ? true : false, $this->message($result[$format]));

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array('', false, null);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatTitle () {
		$format = 'title';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array('', true, false, null, 1, 1000, '1000', $this->Lipsum->getContent(5, 'plain'), $this->Lipsum->getContent(5, 'html'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		$result = $this->Db->select(array(
			'table' => 'formats',
			'fields' => $format,
			'sort' => 'id DESC',
			'limit' => 1
		));

		$this->assertFalse(preg_match('/<[^>]+>$/', $result[$format]['title']) ? true : false, $this->message($result[$format]['title']));
		$this->assertFalse(preg_match('/<[^>]+>$/', $result[$format]['code']) ? true : false, $this->message($result[$format]['code']));

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array('', false, null, $this->Lipsum->getContent(50, 'plain'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatUrl () {
		$format = 'url';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array(
			false, null, '', 'http://example.com/', 'http://example.com', 'https://example.com', 'example.com',
			'http://en.wikipedia.org/wiki/Document', 'http://en.wikipedia.org/wiki/Document_(album)',
			'http://pt.wikipedia.org/wiki/Guimar%C3%A3es',
			'http://icons3.iconfinder.netdna-cdn.com/data/icons/pool/poolbird.png',
			'http://www.asdf-asdf.com',
			'http://webcache.example.com/search?q=cache:40PxrIMaJfgJ:unesdoc.unesco.org/+document&cd=57&hl=en&ct=clnk&gl=es&lr=lang_es|lang_en&source=www.example.com',
			'http://www.contentquality.com/mynewtester/cynthia.exe?Url1=http://domain.tld/&rptmode=2',
			'https://user:pass@www.somewhere.com:8080/login.php?do=login&style=%23#pagetop',
			'http://user@www.somewhere.com/#pagetop',
			'http://www.somewhere.com:8080/#pagetop',
			'ftp://user:****@somewhere.com:21/',
			'www.example.com/index.php/whatever-i-want/',
			'http://www.example.com/url?q=http://www.trulymail.com/',
			'www.example.com/url?q=http://www.trulymail.com/',
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		// PHP BUG: ttp://www.mail-archive.com/php-bugs@lists.php.net/msg143797.html
		// parse_url return as valid http://example.com:8080i/ URL
		$values = array(
			true, false, null, '', 'string', 'example/search?q=1', 'http:///example.com', 'htt://example.com',
			'http://example:8080', 'http://www.example.com/space here.html', '/main.html'
		);

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testInsertFormatVarchar () {
		$format = 'varchar';

		if (in_array($format, $this->disabled) || ($this->execute && !in_array($format, $this->execute))) {
			return true;
		}

		$this->Errors->clean();
		$this->setQuery();

		// VALID
		$values = array('', true, false, null, 1, 1000, '1000', $this->Lipsum->getContent(5, 'plain'), $this->Lipsum->getContent(5, 'html'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertInternalType('integer', $result, $this->message($value));
		}

		// FAIL
		$this->Db->getTable('formats')->setFormatSettings($format, array('required' => true));

		$values = array('', false, null, $this->Lipsum->getContent(50, 'plain'));

		foreach ($values as $value) {
			$this->query['data'][$format] = $value;

			$result = $this->Db->insert($this->query);

			$this->assertFalse($result, $this->message($value));

			$this->Errors->clean();
		}
	}

	public function testTotals ($Db = '') {
		parent::testTotals($this->Db);
	}
}
